package com.bootdo.kaoqin.thread;

import com.bootdo.reptile.ReptileFactory;
import io.netty.util.NettyRuntime;
import io.netty.util.internal.SystemPropertyUtil;

import java.util.concurrent.LinkedBlockingQueue;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;

public class TaskRunnerPool {


    //最小线程数
    //借用netty一段源码  获取当前cpu数量 * 2
    private static final int MIN = SystemPropertyUtil.getInt(
            "io.netty.eventLoopThreads", NettyRuntime.availableProcessors() * 2);
    //限制为线程池线程最大数量
    private static final int MAX = SystemPropertyUtil.getInt(
            "io.netty.eventLoopThreads", NettyRuntime.availableProcessors() * 5);
    //线程自动回收时间 1秒
    private static final int TIMEOUT = 1;

    //创建线程池 此处不提供配置  默认配置即可
    private static final ThreadPoolExecutor threadFactory
            = new ThreadPoolExecutor
            (MIN, MAX, TIMEOUT, TimeUnit.SECONDS,
                    new LinkedBlockingQueue());


    public static void run(Runnable task){
        threadFactory.execute(task);
    }

    public static void shutdown(){
        threadFactory.shutdown();
    }


}
